package com.hqully.labmanage.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hqully.labmanage.common.enums.ResultEnum;
import com.hqully.labmanage.common.exception.DAOException;
import com.hqully.labmanage.common.exception.ResultException;
import com.hqully.labmanage.domain.dto.DeviceTypeDTO;
import com.hqully.labmanage.domain.dto.PageDTO;
import com.hqully.labmanage.domain.entity.DeviceType;
import com.hqully.labmanage.mapper.DeviceTypeMapper;
import com.hqully.labmanage.service.DeviceTypeService;
import com.hqully.labmanage.service.impl.constant.MapperConst;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 针对数据库表[deviceType]的操作Service
 * </p>
 *
 * @author liulingyu <br>
 * @version 1.0
 * @entity {@link DeviceType}
 * @date 2022-10-13 19:11:02 <br>
 */
@Slf4j
@Service
public class DeviceTypeServiceImpl implements DeviceTypeService {


    @Resource
    private DeviceTypeMapper deviceTypeMapper;

    @Override
    public PageInfo<DeviceTypeDTO> pageDeviceTypes(PageDTO page, DeviceTypeDTO deviceTypeDTO) {
        try {
            PageHelper.startPage(page);
            List<DeviceTypeDTO> deviceTypeList = deviceTypeMapper.listDeviceTypes(deviceTypeDTO);
            return new PageInfo<>(deviceTypeList);
        } catch (Exception e) {
            log.error("PAGE [deviceType] FAIL\nINPUT OBJECT: {}\nREASON: {}", deviceTypeDTO, e.toString());
            throw new ResultException(ResultEnum.ERROR, "查询设备类型分页列表失败");
        }
    }

    @Override
    public List<DeviceTypeDTO> listDeviceTypes(DeviceTypeDTO deviceTypeDTO) {
        try {
            return deviceTypeMapper.listDeviceTypes(deviceTypeDTO);
        } catch (Exception e) {
            log.error("LIST [deviceType] FAIL\nINPUT OBJECT: {}\nREASON: {}", deviceTypeDTO, e.toString());
            throw new ResultException(ResultEnum.ERROR, "查询设备类型列表失败");
        }
    }

    @Override
    public int addDeviceType(DeviceTypeDTO deviceTypeDTO) {
        DeviceType deviceType = new DeviceType();
        BeanUtils.copyProperties(deviceTypeDTO, deviceType);

        int status;
        try {
            status = deviceTypeMapper.addDeviceType(deviceType);
        } catch (DuplicateKeyException e) {
            log.warn("ADD [deviceType] FAIL\nINPUT OBJECT: {}\nREASON: {}", deviceTypeDTO, e.toString());
            throw new ResultException(ResultEnum.DUPLICATE_KEY_CONFLICT, "添加设备类型失败\n原因: 设备类型id已存在");
        } catch (Exception e) {
            log.error("ADD [deviceType] FAIL\nINPUT OBJECT: {}\nREASON: {}", deviceTypeDTO, e.toString());
            throw new ResultException(ResultEnum.ERROR, "添加设备类型失败");
        }
        return status;
    }

    @Override
    public int updateDeviceType(DeviceTypeDTO deviceTypeDTO) {
        DeviceType deviceType = new DeviceType();
        BeanUtils.copyProperties(deviceTypeDTO, deviceType);

        int status;
        try {
            status = deviceTypeMapper.updateDeviceType(deviceType);
            if (status == MapperConst.OBJECT_NULL) {
                throw new DAOException("this [deviceType] record not exist");
            }
        } catch (DAOException e) {
            log.warn("UPDATE [deviceType] FAIL\nINPUT OBJECT: {}\nREASON: {}", deviceTypeDTO, e.toString());
            throw new ResultException(ResultEnum.RESULT_NULL, "更新设备类型失败\n原因: 设备类型不存在");
        } catch (Exception e) {
            log.error("UPDATE [deviceType] FAIL\nINPUT OBJECT: {}\nREASON: {}", deviceTypeDTO, e.toString());
            throw new ResultException(ResultEnum.ERROR, "更新设备类型失败");
        }
        return status;
    }

    @Override
    public int deleteDeviceTypeById(Integer id) {
        int status;
        try {
            status = deviceTypeMapper.deleteDeviceTypeById(id);
            if (status == MapperConst.OBJECT_NULL) {
                throw new DAOException("this [deviceType] record not exist");
            }
        } catch (DAOException e) {
            log.warn("DELETE [deviceType] FAIL\nINPUT OBJECT: {}\nREASON: {}", id, e.toString());
            throw new ResultException(ResultEnum.RESULT_NULL, "删除设备类型失败\n原因: 设备类型不存在");
        } catch (Exception e) {
            log.error("DELETE [deviceType] FAIL\nINPUT OBJECT: {}\nREASON: {}", id, e.toString());
            throw new ResultException(ResultEnum.ERROR, "删除设备类型失败");
        }
        return status;
    }


}



